/**
 * 贷款审批
 */
//是否提交贷款
var isSub = false;

//初始化表单
var initForm = function (){
	//初始化审批表单
	$('#approveForm').form({
		url :'sys/Loan/addOrUpdate',
		onBeforeLoad : function(param){
			$.messager.progress();
		},
		onLoadSuccess : function(data) {
			
			$.messager.progress("close");
		},
		onLoadError : function(data) {
			//加载完成之后才构建
			$.messager.progress("close");
			$.dialog.alert("加载内容失败,请重新刷新页面/联系管理员");
		},
		onSubmit : function(param) {
			if (!$(this).form("validate")) {
				$.dialog.alert("提交的选项中,有错误内容,请检查");
				return false;
			}
			$.messager.progress();
		},
		success : function(data) {	
			$.messager.progress("close");
			var data = eval('(' + data + ')');
			if(!!!data){
				$.dialog.alert("未知异常");
				return false;
			}
			if(data.state!=200){
				$.dialog.alert(jd.message);
				return false;
			}
			$('#approveForm').form('load',data);
			if(!isSub){
				$.dialog({
	 				icon: 'success.gif',
	 				title: '提示',
	 			    content: '操作成功!',
	 			    ok: function(){
	 			        this.close();
	 			    }
	 			});
			}else{
				submitApprove();
			}
		}
	});
};

//初始化表单数据
var initFormData = function(){
	if(_is_not_empty(_param_id)){
		$('#approveForm').form('load', 'sys/Loan/findByPk?pks=' + _param_id);
		
	}
};

//退回审批
var returnLoan = function(){
	$.dialog.confirm('你确定退回这条审批单吗？', function(){
		$.ajax({
	       	url:'sys/LoanExt/back',
	    	data:{id:_param_id,toStatus:2},
	    	type:'post',
	    	dataType:'json',
	    	success:function(result){
	    	        if(result.state == "200"){
	    	        	$.dialog({
	    	 				icon: 'success.gif',
	    	 				title: '提示',
	    	 			    content: '退回审批单成功!',
	    	 			    ok: function(){
	    	 			    	location.href = 'jsp/loan/approveLoanList.jsp';
	    	 			    }
	    	 			});
	    	        }else{
	    	        	$.dialog.alert('退回审批单失败！');
	    	        }
	    		}
	        });
	}, function(){
//	    $.dialog.tips('执行取消操作');
	});
};

//提交审批
var submitOrder = function(){
	$.dialog.confirm('你确定提交这条审批单吗？', function(){
		isSub = true;
		$('#approveForm').submit();
	});
	
	
};

var submitApprove = function(){
	$.ajax({
       	url:'sys/LoanExt/submitApprove',
    	data:{id:_param_id,loanStatus:5,loanApproverTime:top.nowTime,loanApproverName:loginUser.userFullName,loanApprover:loginUser.id},
    	type:'post',
    	dataType:'json',
    	success:function(result){
    	        if(result.state == "200"){
    	        	$.dialog({
    	 				icon: 'success.gif',
    	 				title: '提示',
    	 			    content: '提交审批成功!',
    	 			    ok: function(){
    	 			    	location.href = 'jsp/loan/approveLoanList.jsp';
    	 			    }
    	 			});
    	        }else{
    	        	$.dialog.alert('提交审批失败！');
    	        }
    		}
        });
};

//初始化
$(function(){
	initForm();
	initFormData();
});